
mod_a <- fixest::feols(avg_score_trust_elect ~ pre_post*validated_election_outcome, data = estimation_data %>% filter(JML == "Diwawancara 3 kali (pada survei gelombang I, II, dan III)"), cluster = ~ID)

mod_b <- fixest::feols(avg_score_democ_support ~ pre_post*validated_election_outcome, data = estimation_data%>% filter(JML == "Diwawancara 3 kali (pada survei gelombang I, II, dan III)"), cluster = ~ID)

mod1 <- fixest::feols(avg_score_dislike_group ~ pre_post*validated_election_outcome, data = estimation_data%>% filter(JML == "Diwawancara 3 kali (pada survei gelombang I, II, dan III)"), cluster = ~ID)
mod2 <- fixest::feols(avg_score_dislike_pol ~ pre_post*validated_election_outcome, data = estimation_data%>% filter(JML == "Diwawancara 3 kali (pada survei gelombang I, II, dan III)"), cluster = ~ID)
mod3 <- fixest::feols(avg_score_trust_inst ~ pre_post*validated_election_outcome, data = estimation_data%>% filter(JML == "Diwawancara 3 kali (pada survei gelombang I, II, dan III)"), cluster = ~ID)
mod4 <- fixest::feols(avg_score_trust_ppl ~ pre_post*validated_election_outcome, data = estimation_data%>% filter(JML == "Diwawancara 3 kali (pada survei gelombang I, II, dan III)"), cluster = ~ID)
mod5 <- fixest::feols(avg_score_external ~ pre_post*validated_election_outcome, data = estimation_data%>% filter(JML == "Diwawancara 3 kali (pada survei gelombang I, II, dan III)"), cluster = ~ID)
mod6 <- fixest::feols(avg_score_internal ~ pre_post*validated_election_outcome, data = estimation_data%>% filter(JML == "Diwawancara 3 kali (pada survei gelombang I, II, dan III)"), cluster = ~ID)


mods <- list(mod_a, mod_b, mod1, mod2, mod3, mod4, mod5, mod6)

table_df <-
  modelsummary(mods,
               stars = c("*" = 0.1, "**" = 0.05, "***" = 0.01),
               coef_map = c(
                 "pre_post" = "Post Election",
                 "validated_election_outcomeNot Elected" = "Outcome: Lost",
                 "pre_post:validated_election_outcomeNot Elected" = "Post x Lost"
               ),
               gof_map = c("nobs", "r.squared"),
               output = "data.frame"
  ) %>%
  mutate(term = ifelse(statistic == "std.error", "", term)) %>%
  select(-c(part, statistic)) %>%
  mutate_all(as.character)


# Append the new row to the table
latex_table <-
  table_df %>%
  kableExtra::kable("latex",
                    booktabs = T,
                    longtable = T,
                    escape = F,
                    cap = "\\label{tab:table_restrict_1}Effect of Electoral Defeat on Outcomes, Restricted Completions",
                    padding = 0,
                    col.names = linebreak(c("", "(1)", "(2)", "(3)", "(4)", "(5)", "(6)", "(7)", "(8)"), c("l", rep("c", 8))),
                    align = c("l", rep("c", 8)),
                    linesep = "") %>%
  kableExtra::row_spec(6, hline_after = T) %>%
  kableExtra::kable_styling(
    font_size = 9,
    latex_options = c("scale_down",
                      "hold_position",
                      "striped")) %>%
  kableExtra::add_header_above(
    header = c(
      " " = 1,
      "Trust" = 1,
      "Support" = 1,
      "Ethnic" = 1,
      "Partisan" = 1,
      "Inst." = 1,
      "Indiv." = 1,
      "External" = 1,
      "Internal" = 1
      
    ),
    align = c("l", rep("c", 8))
  ) %>%
  kableExtra::add_header_above(
    header = c(
      " " = 1,
      "Democracy" = 2,
      "Animosity" = 2,
      "Trust" = 2,
      "Reasons for Outcome" = 2
    ),
    align = c("l", rep("c", 8))
  )
cat("\\renewcommand{\\arraystretch}{0.9}\n", file = "./outputs/tables/table_a7.tex")
cat(latex_table, file = "./outputs/tables/table_a7.tex", append = TRUE)

